package cc.wsyw126.java.stream;

import com.sun.org.apache.xpath.internal.operations.Bool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by junpeng.yang on 16/8/24.
 */
public class GroupingBy {


    public static void main(String[] args) {
        GroupingBy groupingBy = new GroupingBy();
        groupingBy.callMethod();
        System.out.println("---------------");
        groupingBy.isChild();
        System.out.println("------------");
        groupingBy.isChildPartitioningBy();
    }

    private void callMethod() {
        Map<Integer, List<Person>> personGroups = Stream.generate(new PersonSupplier()).
                limit(100).
                collect(Collectors.groupingBy(Person::getAge));
        Iterator it = personGroups.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, List<Person>> persons = (Map.Entry) it.next();
            System.out.println("Age " + persons.getKey() + " = " + persons.getValue().size());
        }
    }


    private void isChild() {
        Map<Boolean, List<Person>> personGroups = Stream.generate(new PersonSupplier()).
                limit(100).
                collect(Collectors.groupingBy(p -> p.getAge()>18));
        Iterator it = personGroups.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Boolean, List<Person>> persons = (Map.Entry) it.next();
            System.out.println("isChild ? " + persons.getKey() + " = " + persons.getValue().size());
        }
    }

    private void isChildPartitioningBy() {
        Map<Boolean, List<Person>> children = Stream.generate(new PersonSupplier()).
                limit(100).
                collect(Collectors.partitioningBy(p -> p.getAge() < 18));
        System.out.println("Children number: " + children.get(true).size());
        System.out.println("Adult number: " + children.get(false).size());
    }

    private class Person {
        public int no;
        private String name;

        public Person(int i, String s, int i1) {
            this.age = i1;
            this.name = s;
            this.no = i;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        private int age;
        public Person (int no, String name) {
            this.no = no;
            this.name = name;
        }
        public String getName() {
            System.out.println(name);
            return name;
        }
    }

    private class PersonSupplier implements Supplier<Person> {
        private int index = 0;
        private Random random = new Random();

        @Override
        public Person get() {
            return new Person(index++, "StormTestUser" + index, random.nextInt(100));
        }
    }
}
